package com.eeepay.v2_library.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.support.annotation.ColorRes;
import android.text.InputFilter;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.DigitsKeyListener;
import android.text.method.NumberKeyListener;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.eeepay.v2_library.R;

/**
 * Created by Ching on 2016/3/9.
 * 带标签的EditText
 */
public class LabelEditText extends RelativeLayout implements View.OnClickListener {

    private String label, hint, rightBtnText;

    private TextView labelTv, rightTv;
    private EditText editText;
    private RelativeLayout rl_label_editbg;

    private Boolean isEdit;
    private Boolean isNumber;//是否输入数字
    private String isDigits;//是否限制输入字符
    private boolean showRight;//显示右边控件
    private int hintTextColor,labelTextColor,rightTextPadding,maxLength;
    private boolean toLeftGravity;//是否使编辑框内容靠左

    private RightOnClickListener rightClickListener;

    public LabelEditText(Context context) {
        this(context, null);
    }

    public LabelEditText(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LabelEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.LabelEditText);
        //add by zhuangzeqin 2017年8月31日10:48:56 新增自定义属性 isDigits
        isDigits = typedArray.getString(R.styleable.LabelEditText_digits);
        label = typedArray.getString(R.styleable.LabelEditText_labelText);
        hint = typedArray.getString(R.styleable.LabelEditText_hintText);
        rightBtnText = typedArray.getString(R.styleable.LabelEditText_rightBtnText);
        isEdit = typedArray.getBoolean(R.styleable.LabelEditText_isEdit, true);
        isNumber = typedArray.getBoolean(R.styleable.LabelEditText_isNumber, false);
        showRight = typedArray.getBoolean(R.styleable.LabelEditText_showRightBtn, false);
        maxLength = typedArray.getInt(R.styleable.LabelEditText_maxLength, 20);
        hintTextColor = typedArray.getColor(R.styleable.LabelEditText_hintTextColor, getResources().getColor(R.color.label_edittext_righttext_color));
        labelTextColor = typedArray.getColor(R.styleable.LabelEditText_labelTextColor, getResources().getColor(R.color.unify_txt_color_gray));
        toLeftGravity = typedArray.getBoolean(R.styleable.LabelEditText_toLeftGravity, false);
        rightTextPadding = typedArray.getInteger(R.styleable.LabelEditText_rightTextPadding, 0);
        View view = LayoutInflater.from(context).inflate(R.layout.view_label_edittext, this, true);

        labelTv = (TextView) view.findViewById(R.id.tv_let_label);
        editText = (EditText) view.findViewById(R.id.et_input);
        rightTv = (TextView) view.findViewById(R.id.tv_let_right);
        rl_label_editbg = (RelativeLayout) view.findViewById(R.id.rl_label_editbg);

        setViewContent();
        typedArray.recycle();
    }

    private void setViewContent() {

        editText.setEnabled(isEdit ? true : false);
        editText.setFocusableInTouchMode(isEdit ? true : false);
        editText.setFocusable(isEdit ? true : false);

        rightTv.setVisibility(showRight ? View.VISIBLE : View.GONE);

        if (label != null) {
            labelTv.setText(label);
        }
        if (rightBtnText != null) {
            rightTv.setText(rightBtnText);
        }
        if (editText != null) {
            editText.setHint(hint);
        }

        if (hintTextColor != -1) {
            editText.setHintTextColor(hintTextColor);
        }

        if (labelTextColor != -1) {
            labelTv.setTextColor(labelTextColor);
        }


        if (toLeftGravity) {
            editText.setGravity(Gravity.LEFT);
        } else {
            editText.setGravity(Gravity.RIGHT);
        }

        editText.setPadding(rightTextPadding, 0, 0, 0);

        if (isNumber) {
            editText.setInputType(InputType.TYPE_CLASS_NUMBER);
        }
        setFilter(maxLength);

        if (!TextUtils.isEmpty(isDigits))
        {
            setDigits(isDigits);
        }
        rightTv.setOnClickListener(this);
    }

    /**
     * @return 返回标签的文本
     */
    public String getLabel() {
        return labelTv.getText().toString();
    }

    /**
     * @param label 设置标签内容
     */
    public void setLabel(String label) {
        labelTv.setText(label);
    }

    /**
     * @param color 设置标签字体颜色
     */
    public void setLabelTextColor(int color) {
        labelTv.setTextColor(getResources().getColor(color));
    }

    /**
     * 设置背景颜色
     * @param color
     */
    public void setEditbgColor(@ColorRes int color)
    {
        rl_label_editbg.setBackgroundColor(getResources().getColor(color));
    }

    /**
     * 设置编辑文本的颜色
     * @param color
     */
    public void setEditTextColor(int color)
    {
        editText.setTextColor(getResources().getColor(color));
    }

    /**
     * @return 返回Label的TextView
     */
    public TextView getLabelView() {
        return labelTv;
    }

    /**
     * @return 返回编辑框的内容
     */
    public String getEditContent() {
        return editText.getText().toString().trim();
    }

    /**
     * @return 返回编辑框对象
     */
    public EditText getEditText() {
        return editText;
    }

    /**
     * @param content 设置编辑框内容
     */
    public void setEditContent(String content) {
        editText.setText(content);
    }

    /**
     * @param num 设置输入长度限制
     */
    public void setFilter(int num) {
        editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(num)});
    }

    /**
     * 设置输入格式
     * InputType.TYPE_CLASS_TEXT ---> 只能为身份证0123456789Xx
     */
    public void setInputType(int inputType) {
        switch (inputType){
            case InputType.TYPE_CLASS_TEXT:
                editText.setKeyListener(new NumberKeyListener() {
                    @Override
                    public int getInputType() {
                        return InputType.TYPE_CLASS_TEXT;
                    }

                    protected char[] getAcceptedChars() {
                        char numberChars[] = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'X', 'x'};
                        return numberChars;
                    }
                });
                break;
            case InputType.TYPE_NUMBER_FLAG_DECIMAL:
                editText.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
                break;
        }
    }

    public void setDigits(String digits)
    {
        editText.setKeyListener(DigitsKeyListener.getInstance(digits));
    }

    /**
     * @param editListener 设置EditText文本编辑监听
     */
    public void setEditListener(TextWatcher editListener) {
        editText.addTextChangedListener(editListener);
    }

    /**
     * @return 设置焦点
     */
    public boolean setSubViewFocus() {
        return editText.requestFocus();
    }

    /**
     * @param isEdit 设置EditText是否可点击
     */
    public void setEnableEdit(boolean isEdit) {
        editText.setEnabled(isEdit);
    }

    /**
     * @param hintTextColor 设置右侧文本颜色
     */
    public void setHintTextColor(int hintTextColor) {
        editText.setHintTextColor(hintTextColor);
    }

    /**
     * 设置右边输入框的背景
     *
     * @param drawable
     */
    public void setRightBackDrawable(Drawable drawable) {
        if (editText != null) {
            editText.setBackgroundDrawable(drawable);
        }
    }

    /**
     * @param showRight 设置显示右边的控件
     */
    public void showRight(boolean showRight) {
        rightTv.setVisibility(showRight ? View.VISIBLE : View.GONE);
    }

    /**
     * @param rightBtnText 设置右边控件文本
     */
    public void setRightBtnText(String rightBtnText) {
        rightTv.setText(rightBtnText);
    }

    /**
     * @param rightClickListener 设置右边控件监听
     */
    public void setOnRightClickListener(RightOnClickListener rightClickListener) {
        this.rightClickListener = rightClickListener;
    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.tv_let_right) {
            if (rightClickListener !=null){
                rightClickListener.onRightClick(v);
            }
        }
    }

    public interface RightOnClickListener {
        public void onRightClick(View view);
    }

}
